<template>
    <view class="page-creation-draft">
		<u-sticky>
			<!-- 只能有一个根元素 -->
			<view class="search-sticky">
				<u-row gutter="0">
					<u-col span="4" v-for="(item,index) in tab.list" :key="index">
						<view class="text-center">
                            <view class="inline-block" :class="tab.current == index ? 'current' : ''" @click="changeTab(index)">
                                <text class="name">{{ item.name }}</text>
                            </view>
                        </view> 
					</u-col>
				</u-row>
			</view>
		</u-sticky>
        <view class="p-4" v-if="tab.list[tab.current].records.length == 0">
            <u-empty text="暂无内容" mode="list"></u-empty>
        </view> 
        <view class="fly-card">
            <view class="item" v-for="(item,index) in tab.list[tab.current].records" :key="index" @click="gotoPage(item)">
                <view class="content no-border">
                    <view class="title ellipsis-2">{{ item.title || '暂无内容'}}</view>
                    <view class="desc flex-between">
                        <text>{{ item.createTime }}</text>
                        <u-button class="btn-cyan" shape="circle" size="mini" @click.stop="handleDelete(item)"><u-icon name="trash" color="#fff"></u-icon><text class="ml-1">删除</text></u-button>
                    </view>
                </view>
                <view class="thumb">
                    <u-image :src="proxy.$icon.cover1" width="160rpx" height="160rpx"></u-image>
                </view>
            </view>
        </view>
    </view>
</template>
<script setup lang="ts">
import {onShow, onPullDownRefresh, onReachBottom } from '@dcloudio/uni-app';
const {proxy} = getCurrentInstance() as ComponentInternalInstance;
let tab = reactive({
	current: 0,
	list:[
		{id:1, name: '笔记', records: [], current: 1, size: 10, total:0},
		{id:2, name: '推书', records: [], current: 1, size: 10, total:0},
		{id:0, name: '长文', records: [], current: 1, size: 10, total:0},
	]
});
const changeTab = (i) => {
	tab.current = i;
    if(tab.list[tab.current].records.length == 0){
        getList('refresh');
    }
}
const getList = (type) => {
    let obj = tab.list[tab.current];
      if(type == 'refresh'){  
        obj.current = 1;
        obj.list = [];
      }else{
        if(obj.total > obj.records.length){
          obj.current ++;        
        }else{
          uni.showToast({title:'没有更多了',icon: 'none'});
          return;
        }
      };
      let params = {
        current: obj.current,
        size: obj.size,
        articlesType: obj.id,
        status: 0,
      };
      proxy.$http('get', '/admin/articles/page', proxy.$util.getParam(params) ,(data,msg) => {
        obj.records = obj.records.concat(data.records);
        obj.total = data.total;
      },msg => {

      })
};
const gotoPage = (item) => {
    let url = '';
    if(item.articlesType == 0){
        url = '/packageB/creation/article';
    }else if(item.articlesType == 1){
        url = '/packageB/creation/note';
    }else if(item.articlesType == 2){
        url = '/packageB/creation/book';
    };
    proxy.$util.gotoPage(url, {id:item.id});
}
const handleDelete = (item) => {
    uni.showModal({
        title: '提示',
        content: '是否要删除该文章',
        success: res => {
            if(res.confirm){
                proxy.$http('delete', '/admin/articles', [item.id], (data,msg) => {
                    getList('refresh');
                })
            }
        },
        
    })
}
onPullDownRefresh(() => {
	getList('refresh');
	setTimeout( () => {
		uni.stopPullDownRefresh();
	}, 1000);
});
onReachBottom(() =>{
	getList();
});
onShow(() => {
	getList('refresh');
})
</script>